#include "gobanview.hpp"

#include <QPixmap>

GobanView::GobanView(Client * client, GameWindow * gameWindow, int size, Goban * goban) :
        QGraphicsView(gameWindow),
        _gameWindow(gameWindow),
        _client(client),
        _goban(goban),
        _size(size)
{
    _goban->setGobanView(this);
    _scene = new QGraphicsScene(this);

    _offset = 10;
    if(size == 9)
    {
        _squareSide = 51;
        _stoneSize = _squareSide - 10;
        setFixedSize(_size*_squareSide + _stoneSize/2, _size*_squareSide + _stoneSize/2);
    }
    else if(size == 13)
    {
        _squareSide = 35;
        _stoneSize = _squareSide - 7;
        setFixedSize(_size*_squareSide + _stoneSize/2, _size*_squareSide + _stoneSize/2);
    }
    else
    {
        _squareSide = 24;
        _stoneSize = _squareSide - 3;
        setFixedSize(_size*_squareSide + _stoneSize/2, _size*_squareSide + _stoneSize/2);
    }

    _background = new QPixmap("img/" + QString::number(size) + "x" + QString::number(size) + ".png");
    _blackStone = new QPixmap("img/black.png");
    _whiteStone = new QPixmap("img/white.png");
    _ko = new QPixmap("img/ko.png");
    _blackDead = new QPixmap("img/blackdead.png");
    _whiteDead = new QPixmap("img/whitedead.png");
    _blackTerritory = new QPixmap("img/blackterritory.png");
    _whiteTerritory = new QPixmap("img/whiteterritory.png");
    _blackTerritoryWhiteDead = new QPixmap("img/blackterritorywhitedead.png");
    _whiteTerritoryBlackDead = new QPixmap("img/whiteterritoryblackdead.png");


    *_blackStone = _blackStone->scaled(_stoneSize, _stoneSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    *_whiteStone = _whiteStone->scaled(_stoneSize, _stoneSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    *_ko = _ko->scaled(_stoneSize, _stoneSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    *_blackDead = _blackDead->scaled(_stoneSize, _stoneSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    *_whiteDead = _whiteDead->scaled(_stoneSize, _stoneSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    *_blackTerritory = _blackTerritory->scaled(_stoneSize, _stoneSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    *_whiteTerritory = _whiteTerritory->scaled(_stoneSize, _stoneSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    *_blackTerritoryWhiteDead = _blackTerritoryWhiteDead->scaled(_stoneSize, _stoneSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    *_whiteTerritoryBlackDead = _whiteTerritoryBlackDead->scaled(_stoneSize, _stoneSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    _scene->addPixmap(*_background);

    setScene(_scene);


    connect(_goban, SIGNAL(playedAt(int, int, bool)), this, SLOT(addStone(int , int , bool)));
    connect(_goban, SIGNAL(groupDeleted(QVector<QPair<int,int> >)), this, SLOT(deleteGroup(QVector<QPair<int,int> >)));

}

GobanView::~GobanView()
{
    delete _scene;
}

void GobanView::addStone(int x, int y, bool color)
{
    QPair <int, int> pair(x, y);
    if(color)
    {
        QGraphicsPixmapItem * tmp = _scene->addPixmap(*_blackStone);
        tmp->setPos(_squareSide*x - (_stoneSize/2-_offset), _squareSide*y - (_stoneSize/2-_offset));
        _itemVector[pair] = tmp;
    }
    else
    {
        QGraphicsPixmapItem * tmp = _scene->addPixmap(*_whiteStone);
        tmp->setPos(_squareSide*x - (_stoneSize/2-_offset), _squareSide*y - (_stoneSize/2-_offset));
        _itemVector[pair] = tmp;
    }
}

void GobanView::addStone(int x, int y, quint8 color)
{
    QPair <int, int> pair(x, y);

    QGraphicsPixmapItem * tmp;
    if(color == 4)
    {
        tmp = _scene->addPixmap(*_blackTerritoryWhiteDead);
        tmp->setPos(_squareSide*x - (_blackTerritoryWhiteDead->size().width()/2 - _offset),
                    _squareSide*y - (_blackTerritoryWhiteDead->size().width()/2 - _offset));
    }
    else if(color == 5)
    {
        tmp = _scene->addPixmap(*_whiteTerritoryBlackDead);
        tmp->setPos(_squareSide*x - (_whiteTerritoryBlackDead->size().width()/2 - _offset),
                    _squareSide*x - (_whiteTerritoryBlackDead->size().width()/2 - _offset));
    }
    else if(color == 6)
    {
        tmp = _scene->addPixmap(*_whiteTerritory);
        tmp->setPos(_squareSide*x - (_whiteTerritory->size().width()/2 - _offset),
                    _squareSide*y - (_whiteTerritory->size().width()/2 - _offset));
    }
    else if(color == 7)
    {
        tmp = _scene->addPixmap(*_blackTerritory);
        tmp->setPos(_squareSide*x - (_blackTerritory->size().width()/2 - _offset),
                    _squareSide*y - (_blackTerritory->size().width()/2 - _offset));
    }
    else if(color == 8)
    {
        tmp = _scene->addPixmap(*_whiteDead);
        tmp->setPos(_squareSide*x - (_whiteDead->size().width()/2 - _offset),
                    _squareSide*y - (_whiteDead->size().width()/2 - _offset));
    }
    else if(color == 9)
    {
        tmp = _scene->addPixmap(*_blackDead);
        tmp->setPos(_squareSide*x - (_blackDead->size().width()/2 - _offset),
                    _squareSide*y - (_blackDead->size().width()/2 - _offset));
    }
    else
    {
        return;
    }

    _itemVector[pair] = tmp;
}

void GobanView::deleteGroup(const QVector<QPair<int, int> > & group)
{

    for (int i = 0; i < group.size(); ++i)
    {
        if (_itemVector.contains(group[i]))
        {
            _scene->removeItem(_itemVector[group[i]]);

            delete _itemVector[group[i]];
        }
    }
}

void GobanView::mousePressEvent ( QMouseEvent * event )
{
    int squareX, squareY;
    if(event->x() - _offset/2 > 0 && event->y() - _offset/2 > 0)
    {
        squareX = (event->x()/ _squareSide);
        squareY = (event->y()/ _squareSide);

        if(squareX < _size && squareY < _size)
        {
            if (_gameWindow->game()->isStarted() and
                !_gameWindow->game()->isEstablishingScore() and
                ! _gameWindow->game()->isFinished())
            {
                if (_gameWindow->meToPlay() and _gameWindow->game()->goban()->isAValidMove(_gameWindow->amIBlack(), squareX, squareY))
                {
                    _client->send(Mid::PLAYMOVE, _gameWindow->id(), squareX, squareY);
                }
            }
            else if (_gameWindow->game()->isStarted() and
                     _gameWindow->game()->isEstablishingScore() and
                     ! _gameWindow->game()->isFinished())
            {
                if (_goban->map()[squareX][squareY] == 1 or
                    _goban->map()[squareX][squareY] == 2)
                {
                    _client->send(Mid::DEADGROUP, _gameWindow->id(), squareX, squareY);
                }
                else if (_goban->map()[squareX][squareY] == 4 or
                         _goban->map()[squareX][squareY] == 5)
                {
                    _client->send(Mid::ALIVEGROUP, _gameWindow->id(), squareX, squareY);
                }
            }
        }
    }
}

void GobanView::addKO(QPair<int, int> ko)
{
    QGraphicsPixmapItem * tmp = _scene->addPixmap(*_ko);
    tmp->setPos(_squareSide*ko.first, _squareSide*ko.second);
    _itemVector[ko] = tmp;
}
